using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Threading;

using MULENetworking;

namespace MULEServer
{
    public partial class ServerGUI : Form
    {
        
        private int tcpPort = 9887;
        IRemoteServer thisServer;
        //private Thread serverThread;

        public ServerGUI()
        {
            InitializeComponent();
        }

        public void StartServer()
        {
            BinaryClientFormatterSinkProvider clientProvider = null;
            BinaryServerFormatterSinkProvider serverProvider =
               new BinaryServerFormatterSinkProvider();

            serverProvider.TypeFilterLevel =
                System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            IDictionary props = new Hashtable();
            props["port"] = tcpPort;
            props["typeFilterLevel"] = TypeFilterLevel.Full;
            TcpChannel tcpChan = new TcpChannel(
                props, clientProvider, serverProvider);

            ChannelServices.RegisterChannel(tcpChan, false);

            //SIMPLE REMOTING CODE
            /*TcpChannel chan = new TcpChannel(tcpPort);
            ChannelServices.RegisterChannel(chan, false);*/

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteServer),
                            "MULEServer",
                            WellKnownObjectMode.Singleton);
        }

        private void btnStartStop_Click(object sender, EventArgs e)
        {
            tbStatusMsgs.Text += "Starting Server...";
            //serverThread = new Thread(new ThreadStart(StartServer));
            StartServer();
            tbStatusMsgs.Text += "Server Started.\n";
            btnStartStop.Text = "Running...";
            btnStartStop.Enabled = false;

            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            IDictionary props = new Hashtable();
            props["port"] = 0;
            string s = System.Guid.NewGuid().ToString();
            props["name"] = s;
            props["typeFilterLevel"] = TypeFilterLevel.Full;
            TcpChannel tcpChan = new TcpChannel(
                props, clientProvider, serverProvider);

            ChannelServices.RegisterChannel(tcpChan, false);
            thisServer = (RemoteServer)Activator.GetObject(typeof(RemoteServer), "tcp://localhost:9887/MULEServer");
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tbChatDisplay.Text = thisServer.ChatSession;
            lbChatUsers.Items.Clear();
            foreach (string aUser in thisServer.ChatClients)
            {
                lbChatUsers.Items.Add(aUser);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            MessageBox.Show("Are you sure you wish to exit?");
            //serverThread.Abort();
            base.OnClosing(e);
        }

        private void btnKickUser_Click(object sender, EventArgs e)
        {
            string name = null;
            if (lbChatUsers.SelectedIndex > -1)
            {
                name = (string)lbChatUsers.SelectedValue;
                thisServer.KickUser(name);
            }
        }
    }
}